// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Special Ring Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 22 : TYPE_SPECIALRING

private alias 0 : STATE_FLOATING
private alias 1 : STATE_DISAPPEARING

private alias object.type : player.type
private alias object.direction : player.direction
private alias object.controlMode : player.controlMode
private alias object.jumpPress : player.jumpPress
private alias object.jumpHold : player.jumpHold
private alias object.up : player.up
private alias object.down : player.down
private alias object.left : player.left
private alias object.right : player.right
private alias object.value0 : player.rings
private alias object.value16 : player.isPlayer2

// Static Values

// Tables

event ObjectMain

	// Normal state?
	if object.state == STATE_FLOATING

		// Are 50 or more rings collected?
		if player[0].rings > 49

			// Did P1 collide with the special ring?
			BoxCollisionTest(C_TOUCH, object.entityPos, -16, -16, 16, 16, 0, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)

			if checkResult == true

				// Make P1 and its shield "disappear"
				player[0].type = TypeName[Blank Object]
				player[playerCount].type = TypeName[Blank Object]
				player[0].controlMode = -1
				player[0].direction = FLIP_NONE

				ResetObjectEntity(4, TypeName[Blank Object], 0, 0, 0)
				object.drawOrder = 4
				object.state = STATE_DISAPPEARING
				object.frame = 32
				StopSfx(SfxName[Flying])
				StopSfx(SfxName[Tired])
				PlaySfx(SfxName[Special Ring], 0)
				player[0].left = false
				player[0].right = false
				if stage.player2Enabled == true
					if player[1].isPlayer2 == true
						player[1].controlMode = -1
						player[1].up = false
						player[1].down = false
						player[1].left = false
						player[1].right = false
						player[1].jumpPress = false
						player[1].jumpHold = false
					end if
				end if

			end if

		end if

	end if

	// If the player is Super, don't show up
	// -> This should only happen in Debug Mode, since special rings are unloaded
	//    if the player has max emeralds during startup
	if PlayerObject_SuperState == 1
		ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
	end if

end event


event ObjectDraw

	switch object.state
	case STATE_FLOATING
		// Only show up if there are 50 or more rings collected
		if player[0].rings > 49

			temp0 = object.frame
			temp0 >>= 2
			if temp0 > 3
				object.direction = FLIP_X
			else
				object.direction = FLIP_NONE
			end if
			
			DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)

			object.frame++
			if object.frame > 31
				object.frame = 0
			end if

		end if
		break

	case STATE_DISAPPEARING
		// Determine whether to flip the sprite or not based on current frame
		temp0 = object.frame
		temp0 >>= 2
		if temp0 > 11
			object.direction = FLIP_X
		else
			object.direction = FLIP_NONE
		end if

		// Draw the sprite
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)

		if temp0 == 11
			object.direction = FLIP_X
			DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		else
			if temp0 == 15
				object.direction = FLIP_X
				DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
			end if
		end if

		object.frame++
		if object.frame > 63 // Time to disappear?
			// Clear this object
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
		end if
		break

	end switch

end event


event ObjectStartup

	LoadSpriteSheet("Global/SpecialStageRing.gif")

	// Sprite frames
	SpriteFrame(-32, -32, 64, 64, 131, 1)
	SpriteFrame(-32, -32, 64, 64, 1, 66)
	SpriteFrame(-32, -32, 64, 64, 66, 1)
	SpriteFrame(-32, -32, 64, 64, 66, 1)
	SpriteFrame(-32, -32, 64, 64, 1, 1)
	SpriteFrame(-32, -32, 64, 64, 66, 1)
	SpriteFrame(-32, -32, 64, 64, 1, 66)
	SpriteFrame(-32, -32, 64, 64, 66, 1)
	SpriteFrame(-32, -32, 64, 64, 66, 66)
	SpriteFrame(-32, -32, 64, 64, 132, 66)
	SpriteFrame(-32, -32, 64, 64, 1, 131)
	SpriteFrame(-32, -32, 64, 64, 67, 131)
	SpriteFrame(-24, -32, 0, 0, 150, 66)
	SpriteFrame(-16, -32, 0, 0, 101, 66)
	SpriteFrame(0, -32, 0, 0, 222, 1)
	SpriteFrame(-32, -32, 0, 0, 124, 131)

	// Find max emerald count
	if options.superStates == false
		temp0 = 63
	else
		temp0 = 127
	end if

	// Are all emeralds already gathered?
	if specialStage.emeralds >= temp0

		// Big rings aren't needed anymore, delete them
		foreach (TypeName[Special Ring], arrayPos0, ALL_ENTITIES)
			ResetObjectEntity(arrayPos0, TypeName[Blank Object], 0, 0, 0)
		next

	end if

	// Is the game in time attack?
	if options.gameMode == 2
	
		// Delete every big ring, they shouldn't be in time attack
		foreach (TypeName[Special Ring], arrayPos0, ALL_ENTITIES)
			ResetObjectEntity(arrayPos0, TypeName[Blank Object], 0, 0, 0)
		next

	end if

end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Items2.gif")
	SpriteFrame(-32, -32, 64, 64, 83, 1)
end event
